package com.lsp.rocketmqcousumer.receiver;

import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
@RocketMQMessageListener(
        topic = "batchTopic",
        consumerGroup = "BATCH_CONSUME_GROUP_TEST"
)
public class BatchReceiver  implements RocketMQListener<List<MessageExt>> {

    @Override
    public void onMessage(List<MessageExt> list) {
        try {
            System.out.printf("线程：%-25s 接收到新消息 --- %s %n", Thread.currentThread().getName(), "消息大小："+list.size());
            // 设置消息批处理数量后，list中才会有多条，否则每次只会有一条
            for (MessageExt messageExt : list) {
                System.out.printf("线程：%-25s 54 --- %s %n", Thread.currentThread().getName(), new String(messageExt.getBody(), RemotingHelper.DEFAULT_CHARSET));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
